<template>
  <div class="search">
    <!-- 搜索框 -->
    <el-autocomplete @select="getDetail" placeholder="请输入医院名称" class="form" v-model="hosname"
      :fetch-suggestions="fetchData" :trigger-on-focus="false" />
    <el-button type="primary" size="default" @click="" :icon="Search">搜索</el-button>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { Search } from "@element-plus/icons-vue"
//引入接口请求方法
import { reqHospitalGetNAme } from '@/api/home/index'
//引入ts类型规范
import type { HospitalInfo } from '@/api/home/type'
//引入路由
import { useRouter } from "vue-router";
//创建路由器对象
let $router = useRouter()
//收集搜索医院关键字(医院名字)
let hosname = ref<string>('')
//搜索栏发送请求
const fetchData = async (queryString: string, cb: any) => {
  let result: HospitalInfo = await reqHospitalGetNAme(queryString);
  console.log(result.data);
  let showData = result.data.map(item => {
    return {
      value: item.hosname,
      hoscode: item.hoscode
    }
  })
  cb(showData)
}
//跳转详情页
const getDetail = (item: any) => {
  //点击跳转医院详情页,后面需要带query参数
  $router.push({ path: "/hospital/register", query: { hoscode: item.hoscode } });
}
</script>
<script lang="ts">
export default {
  name: 'Search'
}</script>
<style scoped lang="scss">
//引入element-plus 组件 库图标
//CSS深度选择器 >>> 原生   less的 /deep/ scss的 ::v-deep

.search {
  width: 100%;
  height: 50px;
  display: flex;
  
  margin: 20px 0px;

  //深度选择器
  ::v-deep .el-input__inner {
    width: 400px;
    border-radius: 50%;
  }

  .el-button {
    margin-left: 10px
  }
}
</style>
